
#include <QString>
#include "get_setInfo.h"
#include<QDomDocument>
#include<QFile>
#include<QTextStream>
#include<QDebug>
#include<QString>
#include <QMessageBox>
#include<Util/CMethodsInfo.h>
QDomDocument m_doc("mydocument");
bool get_setInfo::openXmlFile(QString FilePath)
{
    QFile file( FilePath );
    if(!file.exists()){
        qWarning()<<FilePath<<"not exist."<<endl;
        return false;
    }
        if( !file.open( QFile::ReadOnly | QFile::Text  ) )
        {
            qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->file.open->%s\n") << FilePath;

            return false;
        }

        if( !m_doc.setContent( &file ) )
        {
            qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->doc.setContent\n") << FilePath;

            file.close();
            return false;
        }
        file.close();
        return true;
}

bool get_setInfo::getEventInfo(QString xmlPath, QList<CState *> *cStateList)
{

    if(!get_setInfo::openXmlFile(xmlPath))
        {
            return false;
        }
    QDomElement root = m_doc.documentElement();

       if(root.tagName()!= "ModelClass")
            return false;
       QDomNode n = root.firstChild();
       while ( !n.isNull() )
       {
           QDomElement e = n.toElement();
           if( !e.isNull())
           {
                       //获取event信息
                       if( e.nodeName() == "Behavior" )
                       {
                           QDomNodeList list = e.childNodes(); //获得元素e的所有子节点的列表
                           for(int a=0; a<list.count(); a++) //遍历该列表
                           {
                               QDomNode node = list.at(a);

                                   if( node.nodeName() == "StateGroup" ){
                                       QDomNodeList list1 = node.childNodes(); //获得元素e的所有子节点的列表
                                       for(int a=0; a<list1.count(); a++) //遍历该列表
                                        {
                                           QDomNode node1 = list1.at(a);
                                           if(node1.nodeName()=="States"){

                                               QDomNodeList list2 = node1.childNodes(); //获得元素e的所有子节点的列表
                                               for(int a=0; a<list2.count(); a++) //遍历该列表
                                                {
                                                    CState *cState=new CState();
                                                       QDomNode node2 = list2.at(a);
                                                       if(node2.nodeName()=="State"){

                                                         QDomNamedNodeMap listQdom2=node2.attributes();
                                                         if(listQdom2.contains("type")&&listQdom2.contains("name")&&listQdom2.contains("description")){
                                                             //获取状态名
                                                             cState->setStateName(listQdom2.namedItem("name").nodeValue());
                                                             //获取状态描述
                                                             cState->setStateDescription(listQdom2.namedItem("description").nodeValue());
                                                             //获取节点类型
                                                             cState->setStateType(listQdom2.namedItem("type").nodeValue());
                                                              }
                                                                 QDomNodeList list3 = node2.childNodes(); //获得元素所有子节点的列表
                                                                 for(int a=0;a<list3.count();a++){
                                                                     QDomNode node3 = list3.at(a);
                                                                     if(node3.nodeName()=="Event"){
                                                                         CEvent *cEvent=new CEvent();
                                                                          QDomNamedNodeMap listQdom=node3.attributes();
                                                                         for(int index=0;index<listQdom.count();index++){
                                                                         if(listQdom.item(index).nodeName()=="name"){
                                                                             cEvent->setEventName(listQdom.item(index).nodeValue());
                                                                          }else if(listQdom.item(index).nodeName()=="type"){
                                                                             cEvent->setEventType(listQdom.item(index).nodeValue());
                                                                          }else if(listQdom.item(index).nodeName()=="description"){
                                                                             cEvent->setEventDescription(listQdom.item(index).nodeValue());

                                                                          }
                                                                         }
                                                                         QDomNodeList list4 = node3.childNodes();
                                                                         for(int i=0;i<list4.count();i++){
                                                                             CParameter *cParameter=new CParameter();
                                                                             cParameter->setNodeName(list4.at(i).nodeName());
                                                                             QDomNamedNodeMap listQdom=list4.at(i).attributes();
                                                                             cParameter->setDatasetReference(listQdom.item(0).nodeValue()); //datasetReference
                                                                             cEvent->addParameter(cParameter);
                                                                         }
                                                                         cState->addEvent(cEvent);

                                                                       }//if(Event)
                                                                 }


                                                           cStateList->append(cState);
                                                   }//if(State)

                                               }

                                               return true;

                                          }//if(States)

                                       }//for

                                   }//if(StateGroup)
                           }
                       }//if(Behavior)

                   }
           n = n.nextSibling();
             }

    return false;


}

bool get_setInfo::addMdlNode(QString xmlPath,QMap<QString,QList<QString> *> *InsertMap){
    if(!get_setInfo::openXmlFile(xmlPath))
        {
            return false;
        }

    QDomElement root = m_doc.documentElement();

       if(root.tagName()!= "ModelClass")
            return false;
       QDomNode n = root.firstChild();

       while ( !n.isNull() )
       {
           QDomElement e = n.toElement();
           if( !e.isNull())
           {
                       //获取event信息
                       if( e.nodeName() == "Runtime" )
                       {
                           QMapIterator<QString, QList<QString> *> i(*InsertMap);
                             while (i.hasNext()) {
                                 i.next();

                                 //添加二级节点
                                 QDomElement secondNode = m_doc.createElement(i.key()); //创建节点                                   
                                 for(int index=0;index<InsertMap->value(i.key())->count();index+=2)
                                 {
                                     //添加三级节点
                                     QDomElement thirdNode=m_doc.createElement("INSERT");
                                     if(i.key()=="HardwareConfigures"||i.key()=="SoftwareConfigures"){


                                         thirdNode.setAttribute("name",InsertMap->value(i.key())->at(index));
                                         QDomText text = m_doc.createTextNode(InsertMap->value(i.key())->at(index+1));
                                         thirdNode.appendChild(text);

                                     }else{
                                         thirdNode = m_doc.createElement("INSERT");
                                         QDomText text = m_doc.createTextNode(InsertMap->value(i.key())->at(index));
                                         thirdNode.appendChild(text);

                                     }
                                      secondNode.appendChild(thirdNode);
                                 }
                                 e.appendChild(secondNode);
                             }
                       }
                   }
              n = n.nextSibling();
             }


    QFile filexml(xmlPath);
    if( !filexml.open( QFile::WriteOnly | QFile::Truncate) ){
        qWarning()<<"error::ParserXML->writeOperateXml->file.open\n"<<endl;
        return false;
           }
           QTextStream ts(&filexml);
           ts.reset();
           ts.setCodec("utf-8");
           m_doc.save(ts, 4, QDomNode::EncodingFromTextStream);
           filexml.close();
           return true;

}

bool get_setInfo::readLyoFile(QString lyoFilePath,QMap<QString,QList<QMap<QString,QString> *> *> *lyoInfo){

    if(!get_setInfo::openXmlFile(lyoFilePath))
        {
            return false;
        }
    QDomElement root=m_doc.documentElement();

    QList<QMap<QString,QString> *> *ActivitysInfoList=new QList<QMap<QString,QString> *>();
    QList<QMap<QString,QString> *> *RulesInfoList=new QList<QMap<QString,QString> *>();



    if(root.tagName()!="WorkFlow")
        return false;
    QDomNode n=root.firstChild();
    while(!n.isNull()){
        QDomElement E=n.toElement();
        if(!E.isNull()){
            //get Activitys info
            if(E.nodeName()=="Activitys"){
                //get all the activitys
                QDomNodeList ActivitysList=E.childNodes();
                for(int index=0;index<ActivitysList.count();index++){
                    QDomNode ActivitysNode=ActivitysList.at(index);
                    if(ActivitysNode.nodeName()=="Activity"){
                        QDomNamedNodeMap ActivityMap=ActivitysNode.attributes();
                        QMap<QString,QString> *ActivitysMap=new QMap<QString,QString>();

                        for(int index1=0;index1<ActivityMap.count();index1++){
                            //get one activity's attributes
                            ActivitysMap->insert(ActivityMap.item(index1).nodeName(),ActivityMap.item(index1).nodeValue());
                        }
                        ActivitysInfoList->append(ActivitysMap);

                    }

                }
            }
            if(E.nodeName()=="Rules"){
                //get all the Rules
                QDomNodeList RulesList=E.childNodes();
                for(int index=0;index<RulesList.count();index++){
                    QDomNode RuleNode=RulesList.at(index);
                    if(RuleNode.nodeName()=="Rule"){
                        QDomNamedNodeMap RuleMap=RuleNode.attributes();
                         QMap<QString,QString> *RulesMap=new QMap<QString,QString>();
                        for(int index1=0;index1<RuleMap.count();index1++){
                            //get one rule's attributes
                            RulesMap->insert(RuleMap.item(index1).nodeName(),RuleMap.item(index1).nodeValue());
                       }
                        RulesInfoList->append(RulesMap);
                    }
                }
            }
        }
         n = n.nextSibling();
    }

    lyoInfo->insert("Activitys",ActivitysInfoList);
    lyoInfo->insert("Rules",RulesInfoList);
    return true;

}

bool get_setInfo::readMethods(QString xmlPath, QList<QString> *CMethods){


    if(!get_setInfo::openXmlFile(xmlPath))
        {
            return false;
        }
    QDomElement root = m_doc.documentElement();
    QDomNodeList DomNodeList =root.childNodes();

    for(int i=0;i<DomNodeList.size();i++){
        QDomNode childNode = DomNodeList.at(i);
        if(childNode.nodeName()=="MethodsFoldList"){
            QDomNodeList MethodsFoldList = childNode.childNodes();
            for(int j=0;j<MethodsFoldList.size();j++){
                 QDomElement MethodFoldDomElem = MethodsFoldList.at(j).toElement();
                QString MethodPath = MethodFoldDomElem.text();
                if(MethodPath!=""){
                  QDir sourceDir =QDir (MethodPath);
                            //获取当前目录所有文件
                            QFileInfoList fileInfoList =sourceDir.entryInfoList();
                            //遍历
                            foreach(QFileInfo fileInfo,fileInfoList){
                                if(fileInfo.fileName()=="."||fileInfo.fileName()=="..") continue;
                                CMethods->append(fileInfo.absoluteFilePath());
                            }

                }

            }
            break;
        }
    }
    return true;

}

bool get_setInfo::readMethods(QString ProjectPath,QString xmlPath, QList<CMethodsInfo *> *CMethods){


    if(!get_setInfo::openXmlFile(xmlPath))
        {
            return false;
        }
    QDomElement root = m_doc.documentElement();
    QDomNodeList DomNodeList =root.childNodes();

    for(int i=0;i<DomNodeList.size();i++){
        QDomNode childNode = DomNodeList.at(i);
        if(childNode.nodeName()=="MethodsFoldList"){
            QDomNodeList MethodsFoldList = childNode.childNodes();
            for(int j=0;j<MethodsFoldList.size();j++){
                 QDomElement MethodFoldDomElem = MethodsFoldList.at(j).toElement();
                QString MethodPath = MethodFoldDomElem.text();
                if(MethodPath!=""){
                    QDir sourceDir;
                    if(QDir(MethodPath).isRelative()){
                       sourceDir=QDir (PathUtil::RelativePathToAbsolutePath(ProjectPath,MethodPath));
                    }else{
                        sourceDir=QDir(MethodPath);
                    }

                            //获取当前目录所有文件
                            QFileInfoList fileInfoList =sourceDir.entryInfoList();
                            CMethodsInfo *mStateInfo = new CMethodsInfo();
                            mStateInfo->setPath(sourceDir.absolutePath());
                            //遍历
                            foreach(QFileInfo fileInfo,fileInfoList){
                                if(fileInfo.fileName()=="."||fileInfo.fileName()=="..") continue;
                                if(QFileInfo (fileInfo.absoluteFilePath()+"\\"+fileInfo.fileName()+".exe").exists()){
                                    mStateInfo->addMethod(fileInfo.absoluteFilePath());
                                }

                            }
                            CMethods->append(mStateInfo);
                }

            }
            break;
        }
    }
    return true;

}

bool get_setInfo::readMethodsFold(QString xmlPath, QList<QString> *CMethodsFolds){

    if(CMethodsFolds==NULL) return false;
    if(!get_setInfo::openXmlFile(xmlPath))
        {
            return false;
        }
    QDomElement root = m_doc.documentElement();
    QDomNodeList DomNodeList =root.childNodes();

    for(int i=0;i<DomNodeList.size();i++){
        QDomNode childNode = DomNodeList.at(i);
        if(childNode.nodeName()=="MethodsFoldList"){
            QDomNodeList MethodsFoldList = childNode.childNodes();
            for(int j=0;j<MethodsFoldList.size();j++){
                 QDomElement MethodFoldDomElem = MethodsFoldList.at(j).toElement();
                QString MethodPath = MethodFoldDomElem.text();
                if(MethodPath!=""){
                    CMethodsFolds->append(MethodPath);
                }
            }
            break;
        }
    }
    return true;
}

bool get_setInfo::setMethodsFold(QString xmlPath, const QList<QString> *CMethodsFolds){

    if(CMethodsFolds==NULL) return false;
    if(!get_setInfo::openXmlFile(xmlPath))
        {
            return false;
        }
    QDomElement root = m_doc.documentElement();
    QDomNodeList DomNodeList =root.childNodes();

    for(int i=0;i<DomNodeList.size();i++){
        QDomNode childNode = DomNodeList.at(i);
        if(childNode.nodeName()=="MethodsFoldList"){
           root.removeChild(childNode);
           childNode=m_doc.createElement("MethodsFoldList");
           root.appendChild(childNode);


            for(int index = 0;index<CMethodsFolds->size();index++){
                QDomElement MethodsToolPathNode=m_doc.createElement("MethodsToolPath");
                QDomText NodeContent = m_doc.createTextNode(CMethodsFolds->at(index));
                MethodsToolPathNode.appendChild(NodeContent);
                childNode.appendChild(MethodsToolPathNode);
               }
            break;
        }
    }

    QFile filexml(xmlPath);
    if( !filexml.open( QFile::WriteOnly | QFile::Truncate) ){
        qWarning()<<"error::ParserXML->writeOperateXml->file.open\n"<<endl;
        return false;
           }
           QTextStream ts(&filexml);
           ts.reset();
           ts.setCodec("utf-8");
           m_doc.save(ts, 4, QDomNode::EncodingFromTextStream);
           filexml.close();
           return true;
}
